%include "I:\workdata\706727\Build\Directories_And_SampleRestrictions.sas";
%include "I:\workdata\706727\Build\Programs.sas";
libname save "I:\Workdata\706727\Build\Data";
libname exp "I:\Workdata\706727\Build\Data";

/* Basedata */
data base (drop=befupdSourceYear);
set raw.befupdv (keep= pnr befupdSourceYear koen efalle kom IE_TYPE FOED_DAG where=(befupdSourceyear>1994 and befupdSourceYear<2015 and befupdSourceyear-year(FOED_DAG)<=60));
year=BEFupdSourceYear;
length year 8;
birthmonth=month(FOED_DAG);
birthyear=year(FOED_DAG);
run;

%macro monthdata; 
  %do t=1 %to 12;
	  data base&t;
	  set base;
	  month=&t;
	  run;
%end;
%mend monthdata;
%monthdata;

data basedata;
set %listsets(base,1,12);
run;

proc datasets;
delete base1 base2 base3 base4 base5 base6 base7 base8 base9 base10 base11 base12;
run;

/* Family link and mental illness history */
data Links;
set raw.befupdv (keep=pnr mor_id far_id );
run;

proc sort data=links;
by pnr descending mor_id far_id;
run;

data links;
set links;
by pnr;
if first.pnr then output;
run;

data links;
set links;
rename mor_id=pnrm;
rename far_id=pnrf;
run;

/* GP visits */

/* 2000-2004 */;
data SYSI;
set raw.SYSIupdv (keep= pnr  speciale ydtyp ydernr afrper  bruhon ydlant where=(
(substr(afrper,1,2)>"94" or substr(afrper,1,2)<"05") and
(substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265" or
ydtyp="93" or ydtyp="94" or speciale="802149" or speciale="802148" or speciale="802147" or speciale="804106" or speciale="806101" or speciale="804003"  or speciale="804063" or speciale="800101"
)));
month=substr(afrper,3,2)*1;
yr=substr(afrper,1,2)*1;
if yr<20 then year=yr+2000;
if yr>20 then year=yr+1900;
length year 8;
if speciale="800101" then docvisit=ydlant;
if (substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265") then psychiatry=ydlant;
if (ydtyp="93" or ydtyp="94") then psyc=ydlant;
if (speciale="802149" or speciale="802148" or speciale="802147") then psyk_test=ydlant;
if (speciale="804106" or speciale="806101" or speciale="804003" or speciale="804063") then therapy=ydlant;
if (substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265") then p_psychiatry=bruhon;
if (ydtyp="93" or ydtyp="94") then p_psyc=bruhon;
if (speciale="802149" or speciale="802148" or speciale="802147") then p_psyk_test=bruhon;
if (speciale="804106" or speciale="806101"  or speciale="804003"  or speciale="804063") then p_therapy=bruhon;
month=substr(afrper,3,2)*1;
run;

/* 2005-2015 */;
data SSSY (  where=(year<2015));
set raw.SSSYupd01v (keep= pnr  speciale ydtyp ydernr afrper  bruhon ydlant where=( (
substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265" or
ydtyp="93" or ydtyp="94" or speciale="802149" or speciale="802148" or speciale="802147" or speciale="804106" or speciale="806101" or speciale="804003"  or speciale="804063" or speciale="800101"
)));
year=substr(afrper,1,2)*1+2000;
length year 8;
if speciale="800101" then docvisit=ydlant;
if (substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265") then psychiatry=ydlant;
if (ydtyp="93" or ydtyp="94") then psyc=ydlant;
if (speciale="802149" or speciale="802148" or speciale="802147") then psyk_test=ydlant;
if (speciale="804106" or speciale="806101" or speciale="804003" or speciale="804063") then therapy=ydlant;
if (substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265") then p_psychiatry=bruhon;
if (ydtyp="93" or ydtyp="94") then p_psyc=bruhon;
if (speciale="802149" or speciale="802148" or speciale="802147") then p_psyk_test=bruhon;
if (speciale="804106" or speciale="806101"  or speciale="804003"  or speciale="804063") then p_therapy=bruhon;
hen19=substr(speciale,1,4)="6301";
hen10=substr(speciale,1,4)="6302";
hen11=substr(speciale,1,4)="6303";
month=substr(afrper,3,2)*1;
run;

data sssy;
set sssy sysi;
run;

proc sort data=SSSY; by pnr year month; run;

proc means data=SSSY  nway noprint;
by pnr year month;
var  docvisit psyc psychiatry psyk_test therapy p_psyc p_psychiatry p_psyk_test p_therapy hen19 hen10 hen11;
output out=GPvisits  sum=;
run;

data GPvisits;
set GPvisits (keep= pnr year month docvisit psyc psychiatry psyk_test therapy p_psyc p_psychiatry p_psyk_test p_therapy hen19 hen10 hen11);
run;

/* Get list of doctors to merge with drug data */
data docs(keep=ydernr spec2);
set sssy(keep=ydernr speciale);
spec2=substr(speciale,1,2);
run;

data docs(keep=ydernr GP psych);
set docs;
GP=(spec2="80");
psych=(spec2="24" or spec2="26");
run;

proc sort data=docs nodup;
by ydernr;
run;

proc datasets;
delete sssy sssy2 sysi;
quit;

/* Psychiatric Hospital */

* Starts *;
data starts (drop=c_adiag d_inddto  where=(year>1994 and year<2015));
set psyc.lpsypop2014 (keep= pnr D_inddto d_hendto c_henm c_pattype c_adiag V_SENGDAGE where=(substr(c_adiag,1,2)="DF")) ;
year=year(d_inddto);
month=month(d_inddto);
if c_pattype="1" then c_pattype="0";
if c_pattype="0" then St_PHosp=substr(c_adiag,3,3)*1;
if (c_pattype="3" and year(d_inddto)<2014) or (c_pattype="2" and d_hendto=d_inddto and c_henm="0" and year(d_inddto)=2014) then ERP=substr(c_adiag,3,3)*1;
erp_s=(c_pattype="3" and year(d_inddto)<2014) or (c_pattype="2" and d_hendto=d_inddto and c_henm="0" and year(d_inddto)=2014);
if c_pattype="0" then St_PHosp_s=V_SENGDAGE;
run;

proc sort data=starts;
by pnr year month ;
run;

proc means data=starts  nway noprint;
by pnr year month ;
var St_PHosp ERP erp_s St_PHosp_s;
output out=starts2 max(St_PHosp)=St_PHosp_1 min(St_PHosp)=St_PHosp_2 max(ERP)=ERP_1 min(ERP)=ERP_2 sum(erp_s)=erp_s sum(st_phosp_s)=st_phosp_s;
run;

data starts ;
set starts2(drop=_TYPE_ _FREQ_) ;
if St_PHosp_1=St_PHosp_2 then St_PHosp_2=.;
if ERP_1=ERP_2 then ERP_2=.;
run;

/* Ambulatory visits */

data psypop;
set psyc.lpsypop2014 (keep= pnr recnum c_adiag c_spec where=(substr(c_adiag,1,2)="DF")) ;
run;

data bes(drop= d_ambdto  where=(year>1994 and year<2015));
set psyc.lpsybes2014 (keep= recnum d_ambdto);
year=year(d_ambdto);
month=month(d_ambdto);
run;

proc sql;
create table ambdata(drop=recnum) as select * from bes as a
left join psypop(where=(recnum ne "")) as b on a.recnum=b.recnum;
quit;

data ambdata(drop=c_adiag c_spec where=(pnr ne ""));
set ambdata;
BAmbD=substr(c_adiag,3,3)*1;
BAmb=1;
run;

proc sort data=ambdata;
by pnr year month ;
run;

proc means data=ambdata  nway noprint;
by pnr year month ;
var BAmb BAmbD;
output out=ambdata2 max(BAmbD)=BAmb_1 min(BAmbD)=BAmb_2 sum(BAmb)=BAmb_s;
run;

data ambdata ;
set ambdata2(drop=_TYPE_ _FREQ_) ;
if BAmb_1=BAmb_2 then BAmb_2=.;
run;

/* Other contacts */
data othercont(keep=pnr year month psui pcont);
set psyc.lpsypop2014(keep= pnr c_adiag d_inddto where=(substr(c_adiag,2,1)="X" or substr(c_adiag,2,1)="Z"));
if substr(c_adiag,2,1)="X" then psui=1;
if substr(c_adiag,2,1)="Z" then pcont=1;
month=month(d_inddto);
year=year(d_inddto);
length year 8;
run;

proc sort data=othercont nodup; by pnr year month; run;

data psykhospfinal;
merge starts ambdata othercont;
by pnr year month;
run;

proc datasets;
delete starts ambdata othercont;
quit;

* Hospital Care  *;
data pop(drop=LPRadmSourceYear);
set raw.LPRadmv (keep=pnr recnum LPRadmSourceYear c_spec c_adiag c_pattype c_kontaars d_inddto
where=(LPRadmSourceYear>1994 and c_spec ne "50" and c_spec ne "52"));
year=year(d_inddto);
month=month(d_inddto);
run;

/* Somatic hospital visits with mental health diag excl. dementia*/
data HospPsyc(keep= pnr year month mentaldiag);
set pop (keep= pnr year month c_adiag where=((substr(c_adiag,1,2)="DF" and substr(c_adiag,1,3)~="DF0")));
mentaldiag=1;
run;

proc sort data=hosppsyc nodup; by pnr year month; run;

proc means data=hosppsyc  nway noprint;
by pnr year month;
var mentaldiag;
output out=hosppsycfinal  max=;
run;

data hosppsycfinal;
set hosppsycfinal (keep=pnr year month mentaldiag);
run;

proc datasets;
delete hosppsyc;
quit;

/* Suicide attempts and successful suicides*/

%macro wordcount(list);
%local count;
%let count=0;
%do %while(%qscan(&list.,&count.+1,%str( )) ne %str() );
 %let count=%eval(&count.+1);
%end;
&count.
%mend wordcount;

%macro diaglist(defvar,varlist);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
&defvar.="&var." or
%end;
%mend;

data suiatt(drop=c_adiag where=(asui=1 or pois=1));
set pop (keep=pnr recnum d_inddto year month c_pattype c_kontaars c_adiag);
diag1=substr(c_adiag,2,1);
diag2=substr(c_adiag,2,2);
Asui=(c_kontaars="4"); 
pois=(substr(c_adiag,1,4)="DT39" or substr(c_adiag,1,4)="DT42" or substr(c_adiag,1,4)="DT43");
pois_painkiller=substr(c_adiag,1,4)="DT39";

* Change pattype to fix prices ;
if c_pattype="2" then c_pattype="3";
if c_pattype="1" then c_pattype="3";
run;

/* Remove duplicates on the same day */
proc sort data=suiatt;
by pnr d_inddto c_kontaars c_pattype;
run;

data suiatt2;
set suiatt;
by pnr d_inddto c_kontaars;
if first.c_kontaars then output; 
run;

proc sql;
create table suiatt3(keep=pnr year month asui pois pois_painkiller c_pattype count1 count2 mprice3 mprice2 mprice1) as select * from suiatt2 as a

left join exp.suiavprice1(keep=c_pattype diag1 mprice1 _freq_ rename=(_freq_=count1)) as b on  a.c_pattype=b.c_pattype and a.diag1=b.diag1
left join exp.suiavprice2(keep=c_pattype diag2 mprice2 _freq_ rename=(_freq_=count2)) as c on  a.c_pattype=c.c_pattype and a.diag2=c.diag2
left join exp.suiavprice3(keep=c_pattype mprice3) as d on  a.c_pattype=d.c_pattype; 
quit;

data suiatt3;
set suiatt3;
if count2>=5 then p_asui=coalesce(mprice2,mprice1);
if count2<5 and count1>=5 then p_asui=mprice1;
if count2<5 and count1<5 then p_asui=mprice3;
if asui=0 then p_asui=.;
run;

proc sort data=suiatt3;
by pnr year month;
run;

proc means data=suiatt3  nway noprint;
by pnr year month;
var Asui pois pois_painkiller p_asui;
output out=suiattfinal  sum=;
run;

data suiattfinal;
set suiattfinal(keep=pnr year month asui pois pois_painkiller p_asui);
run;

proc datasets;
delete suiatt ;
quit;

* Mortality *;
data dod1(keep=pnr year month c_liste49);
set raw.dodsaarsv(keep=pnr d_dodsdto c_liste_49);
year=year(d_dodsdto);
month=month(d_dodsdto);
rename c_liste_49=c_liste49;
run;

data dod2(keep=pnr year month c_liste49);
set raw.dodsaasgv(keep=pnr d_statdato c_liste49);
year=year(d_statdato);
month=month(d_statdato);
run;

* Assign average price of suicide attempt hospitalizations *;
data dodprice(keep=mergevar p_dsui);
set exp.suiavprice3 (where=(c_pattype="0"));
rename mprice3=p_dsui;
mergevar=1;
run;

data dod;
set dod1 dod2;
mergevar=1;
run;

proc sql;
create table dodfinal(drop=mergevar ) as select * from dod as a

left join dodprice as b on  a.mergevar=b.mergevar;
quit;

proc datasets;
delete pop dod1 dod2 diag;
quit;

/* Medicine data */
%macro meddata; 
  %do t=1995 %to 2014;
	  data med&t(keep=pnr month year anxiety sleep adep ssri psychostim antipsych psykmed p_adep p_anxiety p_sleep recu_ydernr);
	  set lmdb.lmdb&t.(where=(substr(ATC,1,3)="N05" or (substr(ATC,1,3)="N06" & substr(ATC,1,7)~="N06AX12"))); /* exclude buproprion because it is used for smoking cessation */
	  month=month(eksd);
	  year=year(eksd); 
	  Adep=(substr(ATC,1,4)="N06A");
	  SSRI=(substr(ATC,1,5)="N06AB");
	  anxiety=(substr(ATC,1,4)="N05B");
	  sleep=(substr(ATC,1,4)="N05C");
	  psychostim=(substr(ATC,1,4)="N06B");
	  antipsych=(substr(ATC,1,4)="N05A");
	  psykmed=1;
	  if ibgp~=. then price=ibgp-ptp;
	  if ibgp=. then price=0;
	  if price<0 then price=0;
	  p_adep=adep*price;
	  p_anxiety=anxiety*price;
	  p_sleep=sleep*price;
	  run;
%end;
%mend meddata;
%meddata;

data meddata;
set %listsets(med,1995,2014);
run;

proc sql;
create table docmeddata as select * from meddata as a

left join docs as b on a.recu_ydernr=b.ydernr;
quit;

data docmeddata;
set docmeddata;
if GP~=1 then GP=0;
if psych~=1 then psych=0;
GPadep=GP*adep;
psychadep=psych*adep;
GPssri=GP*ssri;
psychssri=psych*ssri;
GPanxiety=GP*anxiety;
psychanxiety=psych*anxiety;
GPsleep=GP*sleep;
psychsleep=psych*sleep;
GPpsychostim=GP*psychostim;
psychpsychostim=psych*psychostim;
GPantipsych=GP*antipsych;
psychantipsych=psych*antipsych;
run;

proc sort data=docmeddata;
by pnr  year month;
run;

proc means data=docmeddata nway noprint;
var p_adep p_anxiety p_sleep psykmed adep ssri  anxiety sleep antipsych psychostim GPadep psychadep GPssri psychssri GPanxiety psychanxiety GPsleep psychsleep GPpsychostim psychpsychostim GPantipsych psychantipsych;
by pnr year month;
output out=finalmeddata max(psykmed ssri antipsych psychostim GPadep psychadep GPssri psychssri GPanxiety psychanxiety GPsleep psychsleep GPpsychostim psychpsychostim GPantipsych psychantipsych)=
sum(adep anxiety sleep p_adep p_anxiety p_sleep)=;
run;

data finalmeddata(drop=GPadep psychadep GPssri psychssri GPanxiety psychanxiety GPsleep GPpsychostim psychpsychostim GPantipsych psychantipsych psychsleep prescrib1 prescrib2 prescrib3 prescrib4 prescrib5);
set finalmeddata;
length prescrib0 $6;
if adep=0 then prescrib0="0     ";
if adep>0 and GPadep=1 then prescrib0="1     ";
if adep>0 and GPadep=0 and psychadep=1  then prescrib0="2     ";
if adep>0 and GPadep=0 and psychadep=0  then prescrib0="3     ";
length prescrib1 $6;
if ssri=0 then prescrib1=tranwrd(prescrib0,"     ","0    ");
if ssri>0 and GPssri=1 then prescrib1=tranwrd(prescrib0,"     ","1    ");
if ssri>0 and GPssri=0 and psychssri=1  then prescrib1=tranwrd(prescrib0,"     ","2    ");
if ssri>0 and GPssri=0 and psychssri=0  then prescrib1=tranwrd(prescrib0,"     ","3    ");
length prescrib2 $6;
if anxiety=0  then prescrib2=tranwrd(prescrib1,"    ","0   ");
if anxiety>0 and GPanxiety=1 then prescrib2=tranwrd(prescrib1,"    ","1   ");
if anxiety>0 and GPanxiety=0 and psychanxiety=1  then prescrib2=tranwrd(prescrib1,"    ","2   ");
if anxiety>0 and GPanxiety=0 and psychanxiety=0  then prescrib2=tranwrd(prescrib1,"    ","3   ");
length prescrib3 $6;
if sleep=0 then prescrib3=tranwrd(prescrib2,"   ","0  ");
if sleep>0 and GPsleep=1 then prescrib3=tranwrd(prescrib2,"   ","1  ");
if sleep>0 and GPsleep=0 and psychsleep=1  then prescrib3=tranwrd(prescrib2,"   ","2  ");
if sleep>0 and GPsleep=0 and psychsleep=0  then prescrib3=tranwrd(prescrib2,"   ","3  ");
length prescrib4 $6;
if psychostim=0 then prescrib4=tranwrd(prescrib3,"  ","0 ");
if psychostim>0 and GPpsychostim=1 then prescrib4=tranwrd(prescrib3,"  ","1 ");
if psychostim>0 and GPpsychostim=0 and psychpsychostim=1  then prescrib4=tranwrd(prescrib3,"  ","2 ");
if psychostim>0 and GPpsychostim=0 and psychpsychostim=0  then prescrib4=tranwrd(prescrib3,"  ","3 ");
length prescrib5 $6;
if antipsych=0 then prescrib5=tranwrd(prescrib4," ","0");
if antipsych>0 and GPantipsych=1 then prescrib5=tranwrd(prescrib4," ","1");
if antipsych>0 and GPantipsych=0 and psychantipsych=1  then prescrib5=tranwrd(prescrib4," ","2");
if antipsych>0 and GPantipsych=0 and psychantipsych=0  then prescrib5=tranwrd(prescrib4," ","3");
prescrib=prescrib5;
run;

proc datasets;
delete med1995 med1996 med1997 med1998 med1999 med2000 med2001 med2002 med2003 med2004 med2005 med2006 med2007 med2008
	   med2009 med2010 med2011 med2012 med2013 med2014 meddata;
quit;

/* Merge Data */
proc sql;
create table merged( where=(pnr ne "" and year ne . and year>1994) ) as select * from basedata as a
left join GPvisits as b on a.pnr=b.pnr and a.year=b.year and a.month=b.month
left join psykhospfinal as c on a.pnr=c.pnr and a.year=c.year and a.month=c.month
left join hosppsycfinal as d on a.pnr=d.pnr and a.year=d.year and a.month=d.month
left join suiattfinal as e on a.pnr=e.pnr and a.year=e.year and a.month=e.month
left join dodfinal as f on a.pnr=f.pnr and a.year=f.year and a.month=f.month
left join finalmeddata as g on a.pnr=g.pnr and a.year=g.year and a.month=g.month;
quit;

/* Family history */
data famvars;
set merged (keep=pnr year month psykmed Asui mentaldiag bamb_1 erp_1 st_phosp_1 psyc psychiatry psyk_test therapy);
any=(psykmed>0 or Asui>0 or mentaldiag>0 or bamb_1>0 or erp_1>0 or st_phosp_1>0 or psyc>0 or psychiatry>0 or psyk_test>0 or therapy>0);
yrm=year+(month-1)/12;
run;

%macro lags; 
 %do i=2 %to 60;
  %let j=%eval(&i.-1);
 lagpnr&i.=lag(lagpnr&j.);
 lagyrm&i.=lag(lagyrm&j.);
 lagany&i.=lag(lagany&j.);
 if pnr=lagpnr&i. and yrm-lagyrm&i.<=5 and lagany&i.~=.  then famany=famany+lagany&i.;
%end;
%mend lags;

proc sort data=famvars;
by pnr year month;
run;

data famvarsfinal(keep=pnr year month famany);
set famvars;
lagpnr1=lag(pnr);
lagyrm1=lag(yrm);
lagany1=lag(any);
famany=0;
if pnr=lagpnr1 and yrm-lagyrm1<=5 and lagany1~=.  then famany=famany+lagany1;
%lags;
run;

proc sql;
create table famlinkvars_mom(where=(pnrm~="            " and famany_mom~=.)) as select pnrm,year,month,sum(famany) as famany_mom from links(keep=pnr pnrm) as a
left join famvarsfinal as b on a.pnr=b.pnr 
group by pnrm, year, month;
quit;

proc sql;
create table famlinkvars_dad(where=(pnrf~="            " and famany_dad~=.)) as select pnrf,year,month,sum(famany) as famany_dad from links(keep=pnr pnrf) as a
left join famvarsfinal as b on a.pnr=b.pnr
group by pnrf, year, month;
quit;

proc sql;
create table famlinkfinal1 as select * from links as a

left join famvarsfinal as b on a.pnr=b.pnr ;
quit;

proc sql;
create table famlinkfinal2 as select * from famlinkfinal1 as a

left join famvarsfinal(rename=(pnr=pnrm famany=famany_m)) as b on a.pnrm=b.pnrm and a.year=b.year and a.month=b.month
left join famvarsfinal(rename=(pnr=pnrf famany=famany_f)) as c on a.pnrf=c.pnrf and a.year=c.year and a.month=c.month
left join famlinkvars_mom as d on a.pnrm=d.pnrm and a.year=d.year and a.month=d.month
left join famlinkvars_dad as e on a.pnrf=e.pnrf and a.year=e.year and a.month=e.month
left join famlinkvars_mom(rename=(famany_mom=famchild_mom)) as f on a.pnr=f.pnrm and a.year=f.year and a.month=f.month
left join famlinkvars_dad(rename=(famany_dad=famchild_dad)) as g on a.pnr=g.pnrf and a.year=g.year and a.month=g.month;
quit;

data famlinkfinal2(keep=pnr year month famany famany_par famchild_dad famchild_mom famany_sib);
set famlinkfinal2;
if famany~=. then famany_mom=famany_mom-famany;
if famany~=. then famany_dad=famany_dad-famany;
famany=(famany>0);
famany_par=(famany_f>0 or famany_m>0);
famany_sib=(famany_mom>0 or famany_dad>0);
run;

/*** Final Merge ***/
proc sql;
create table PsycHighFreq_AllAges as select * from  merged as a 

left join famlinkfinal2 as b on a.pnr=b.pnr and a.year=b.year and a.month=b.month;
quit;

data PsycHighFreq_AllAges(drop=famchild_dad famchild_mom);
set PsycHighFreq_AllAges;
if koen=1 then famchild=famchild_dad;
if koen=2 then famchild=famchild_mom;
famany_child=(famchild>0);
run;


/** Export **/
proc sort data=PsycHighFreq_AllAges nodup; by pnr year month; run;

proc export data=PsycHighFreq_AllAges
outfile = "J:\Workdata\706727\Temp\PsycHighFreq_AllAges.dta"
replace;
run;


